package com.aier.proxy.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.aier.admin.entity.IcCard;
import com.aier.admin.entity.mapper.IcCardRowMapper;
import com.aier.common.common.Constant;
import com.aier.common.dao.ICommonDAO;
import com.aier.entrance.common.EntranceICFinalData;
import com.aier.proxy.service.IUserAssignCardService;
import com.aier.proxy.service.IUserBindIcardService;
import com.aier.school.entity.SchoolIcCard;
import com.aier.school.entity.mapper.SchoolIcCardRowMapper;
import com.aier.user.common.UserIcardData;

@Component("userAssignCardService")
public class UserAssignCardServiceImpl_J implements IUserAssignCardService {
    @Resource(name = "commonDao")
    private ICommonDAO commonDao;

    @Resource(name = "userBindIcardService")
    private IUserBindIcardService userBindIcardService;

    public void setCommonDao(ICommonDAO commonDao) {
        this.commonDao = commonDao;
    }

    public void setUserBindIcardService(
            IUserBindIcardService userBindIcardService) {
        this.userBindIcardService = userBindIcardService;
    }

    /**
     * 向学校添加代理卡
     */
    @Override
    public int insertSchool(Integer sid, Long icid) {
        SchoolIcCard schoolIcard = new SchoolIcCard();
        schoolIcard.setIcid(icid);
        schoolIcard.setSid(sid);
        // schoolIcard.setState(UserIcardData.SI_STATE_NOTACTIVATED);
        schoolIcard.setState(UserIcardData.SI_STATE_ON);
        try {
            commonDao.insert("school_icard", SchoolIcCardRowMapper.getPar(
                    new HashMap<String, Object>(), schoolIcard));
            commonDao.update("update ic_card set icstate=? where icid=?",
                    new Object[] { EntranceICFinalData.IC_STATE_SCHOOL, icid });
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    // 将IC卡分派给学校
    @Override
    public int insertICardToSchool(Integer pid,String ichead, String icbodyMin,
            String icbodyMax, Integer sid) {
        List<IcCard> icared = userBindIcardService.searchIcidByIcBody(pid,ichead,
                icbodyMin, icbodyMax);
        for (IcCard ic : icared) {
            insertSchool(sid, ic.getIcid());
        }
        return Constant.num_10001;
    }

    // 查询代理商拥有的IC卡
    @Override
    public Map<String, Object> serachIcardsByPid(Integer pid, int page,
            int page_size) {
        if (null == pid) {
            return null;
        }
        StringBuffer sql = new StringBuffer(
                "select * from ic_card as ic left join proxy_icard as pi on ic.icid=pi.icid " +
                "where pi.pid=? and (ic.icstate=? or ic.icstate=?) order by ic.icstate");
        List<Object> lo = new ArrayList<Object>();
        lo.add(pid);
        lo.add(EntranceICFinalData.IC_STATE_PROXY);
        lo.add(EntranceICFinalData.IC_STATE_SCHOOL);

        return commonDao.queryPage(sql.toString(), page, page_size, lo,
                new IcCardRowMapper());

    }
    
    
    

    @SuppressWarnings("unchecked")
    @Override
    public List<IcCard> serachUnUseIcardsByPid(Integer pid) {
        return (List<IcCard>) commonDao
                .list("select * from ic_card as ic left join proxy_icard as pi " +
                		"on ic.icid=pi.icid where pi.pid=? and ic.icstate=? order by ic.icstate",
                        new Object[] { pid, EntranceICFinalData.IC_STATE_PROXY },
                        new IcCardRowMapper());

    }

    /**
     * 查询该代理商的未分派的IC卡
     */
    @Override
    public Map<String, Object> serachUnAssignIcardsByPid(Integer pid,
            String ichead, String icbodyMin, String icbodyMax, int page,
            int page_size) {
        if (null == pid) {
            return null;
        }
        StringBuffer sql = new StringBuffer(
                "select ic.*,CONCAT(ic.ichead,ic.icbody) as icName from ic_card as ic left join proxy_icard as pi " +
                "on ic.icid=pi.icid where pi.pid=? and ic.icstate=? and ic.ichead=? and (?<=ic.icbody and ?>=ic.icbody) order by ic.icstate");
        List<Object> lo = new ArrayList<Object>();
        lo.add(pid);
        lo.add(EntranceICFinalData.IC_STATE_PROXY);
        lo.add(ichead);
        lo.add(icbodyMin);
        lo.add(icbodyMax);

        return commonDao.queryPage(sql.toString(), page, page_size, lo,
                new IcCardRowMapper(IcCardRowMapper.AND_ICNAME));

    }
    @SuppressWarnings("unchecked")
    @Override
    public List<IcCard> serachUnAscardsByPid(Integer pid, String ichead,
            String icbodyMin, String icbodyMax) {
        List<Object> lo = new ArrayList<Object>();
        lo.add(pid);
        lo.add(EntranceICFinalData.IC_STATE_PROXY);
        lo.add(ichead);
        lo.add(icbodyMin);
        lo.add(icbodyMax);
        return (List<IcCard>) commonDao.list("select ic.*,CONCAT(ic.ichead,ic.icbody) as icName from ic_card as ic " +
        		"left join proxy_icard as pi on ic.icid=pi.icid where pi.pid=? and ic.icstate=? and ic.ichead=? and " +
        		"(?<=ic.icbody and ?>=ic.icbody) order by ic.icstate", lo, new IcCardRowMapper(IcCardRowMapper.AND_ICNAME));
    }
    /**
     * 查询某系列IC数量
     */
    @Override
    public int searchCountByichead(Integer pid, String ichead) {
        return commonDao
                .countByQuali(
                        "select * from ic_card as ic left join proxy_icard as pi on ic.icid=pi.icid where pi.pid=? and ic.icstate=? and ic.ichead=?",
                        new Object[] { pid, EntranceICFinalData.IC_STATE_PROXY,ichead });
    }

    

}
